package com.ericsson.couponingclient;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import com.ericsson.couponing.Campaign;
import com.ericsson.couponing.ConfigureCampaignRequest;
import com.ericsson.couponing.ConfigureCampaignResponse;
import com.ericsson.couponing.CouponingService;
import com.ericsson.couponing.CouponingServicePort;

public class ConfigureCampaignServlet extends HttpServlet {
	
	private static final long serialVersionUID = 8401828503357323242L;
	
//	@WebServiceRef(name="CouponingService", value=CouponingService.class)
//	private static CouponingService service;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("ciao");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//String startTimestamp = req.getParameter("startTimestamp");
		//String endTimestamp = req.getParameter("endTimestamp");
		
		CouponingService service = new CouponingService();
		
		Campaign campaign = new Campaign();
		campaign.setCampaignDescription(req.getParameter("description"));
		campaign.setProvisioningCode(req.getParameter("provisioningCode"));
		campaign.setVenditorePrimario(req.getParameter("venditorePrimario"));
		campaign.setSmsTextPresaInCarico(req.getParameter("smsTextPresaInCarico"));
		campaign.setSmsTextCampagnaScaduta(req.getParameter("smsTextCampagnaScaduta"));
		campaign.setSmsTextPinErrato(req.getParameter("smsTextPinErrato"));
		
		
		try {
			campaign.setCampaignStartTimestamp(convertDateFromString(req.getParameter("startTimestamp")));
			campaign.setCampaignEndTimestamp(convertDateFromString(req.getParameter("endTimestamp")));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DatatypeConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ConfigureCampaignRequest request = new ConfigureCampaignRequest();
		request.setCampaign(campaign);
		
		CouponingServicePort port = service.getCouponingServiceSOAP();
		
		ConfigureCampaignResponse response = port.configureCampaign(request);
		System.out.println(response.getResultCode());
		
	}
	
	
	private XMLGregorianCalendar convertDateFromString(String timestamp) throws ParseException, DatatypeConfigurationException {
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
		Date date = dateFormat.parse(timestamp);
		GregorianCalendar calendar = new GregorianCalendar();		
		calendar.setTime(date);
		XMLGregorianCalendar xmlDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
		
		return xmlDate;
	}

}
